package repository

import (
	"context"
	"elegantGo/chapter17/oo"
	"elegantGo/chapter17/repository/ent"
	"elegantGo/chapter17/repository/ent/vip"
)

type Vip struct {
	db *ent.Client
}

func NewVip(db *ent.Client) *Vip {
	repo := new(Vip)
	repo.db = db
	return repo
}

func (repo *Vip) Fetch(ctx context.Context, id int) oo.Vip {
	return repo.FetchOne(ctx, func(opt *ent.VipQuery) {
		opt.Where(vip.ID(id))
	})
}

func (repo *Vip) FetchOne(ctx context.Context, optionFunc func(opt *ent.VipQuery)) oo.Vip {
	builder := repo.db.Vip.Query()
	optionFunc(builder)
	return builder.FirstX(ctx).Mapper()
}

func (repo *Vip) FetchMany(ctx context.Context, optionFunc func(opt *ent.VipQuery)) oo.Vips {
	builder := repo.db.Vip.Query()
	optionFunc(builder)
	var entVips ent.Vips = builder.AllX(ctx)
	return entVips.Mapper()
}
